package com.woniu.controller;

import com.woniu.dto.HotCommentsDTO;
import com.woniu.dto.HotDetailDTO;
import com.woniu.dto.HotListDTO;
import com.woniu.pojo.*;
import com.woniu.service.DynamicService;
import com.woniu.service.HotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/hot")
@Controller
@CrossOrigin
public class HotController {
    @Autowired
private HotService hotService;
@Autowired
private DynamicService dynamicService;
    /**
     * 活动发布
     * @param hotList
     * @return
     */
    @PostMapping("/hotRelease")
    @ResponseBody
    public Map hotRelease(HotList hotList){
        hotList.setCreateTime(new Date());
        //System.out.println(hotList);
        String pic=hotList.getPicture();
        //去掉图片地址最后一个逗号
        String picture=pic.substring(0,pic.length()-1);
        hotList.setPicture(picture);
        hotList.setStatus(0);
       Map map= new HashMap();
        try {
            hotService.addHot(hotList);
            map.put("code",200);
            return  map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","活动发布失败!");
            return  map;
        }
    }

    /**
     * 活动展示
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/hotList")
    @ResponseBody
    public Map hotList(@RequestParam(value = "page", defaultValue = "1", required = false) Integer currentPage,
                    @RequestParam(value = "limit", defaultValue = "100", required = false) Integer pageSize) {
        List<HotListDTO> hotList=hotService.selectDTOByPage(currentPage,pageSize);

        Map map = new HashMap();
        map.put("hotList",hotList);
        return map;
    }



    /**
     * 展示最新的两个活动
     * @return
     */
    @ResponseBody
    @RequestMapping("/latestHot")
    public  Map latestHot(){
        Map map = new HashMap();
        List<HotListDTO> latestHot= hotService.selectLastestHot();
        map.put("latestHot",latestHot);
        return map;
    }


    /**
     * 展示还有的活动
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/friendHotList/{userId}")
    public Map friendHotList(@PathVariable("userId") Long userId){
       // System.out.println("好友活动："+userId);
        Map map = new HashMap();
        List<Long> friendsId=dynamicService.selectFriendsId(userId);
        List<HotListDTO> friendHotList=hotService.selectFriendHot(friendsId);

        map.put("friendHotList",friendHotList);
        return map;
    }








    /**
     * 活动详情
     * @param hotId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/showHotDetail/{hotId}/{userId}")
    public Map showHotDetail(@PathVariable("hotId") Long hotId,@PathVariable("userId") Long userId){
//        System.out.println("活动id："+hotId);
//        System.out.println("用户id："+userId);
        Map map = new HashMap();
        HotDetailDTO hotDetailDTO =hotService.selectHotDetail(hotId);
        map.put("hotDetail", hotDetailDTO);
        //判断用户是否对该活动进行了收藏
        HotCollect collect=hotService.selectHotCollect(hotId,userId);
       if (collect!=null){
           //1：自定义，前段判断
           map.put("collectStatus",1);
       }
      List<HotCommentsDTO> comments=hotService.selectHotComment(hotId);
        map.put("comments",comments);
        return map;
    }


    /**
     * 活动报名
     * @param hotId
     * @return
     */
    @ResponseBody
    @RequestMapping("/hotApply/{hotId}/{userId}")
    public Map hotApply(@PathVariable("hotId") Long hotId,@PathVariable("userId") Long userId){
       // System.out.println("活动id:"+hotId);
       // System.out.println("用户id:"+userId);
        Map map = new HashMap();
        //查询活动里是否有该用户
        HotListDetail detail=hotService.selectHot(hotId,userId);
        if(detail!=null){
            //201:自定义状态，表示你已经报过名了
            map.put("code",201);
            return map;
        }
        //查询已报名人数
        Integer realNum=hotService.selectRealNum(hotId);
        //查询活动需要的人数
        Integer personNum=hotService.selectPersonNum(hotId);
        if(realNum>=personNum){
            //202：表示活动人数已满
            map.put("code",202);
            return map;
        }

        //判断报名时间，活动开始前30分钟内不能再报名
        Date startTime=hotService.selectTime(hotId);
        Calendar hotStartTime=Calendar.getInstance();
        hotStartTime.setTime(startTime);
        Calendar hotEndTime =Calendar.getInstance();
        hotEndTime.add(Calendar.MINUTE,30);
        if(hotEndTime.after(hotStartTime)){
            //203：表示活动报名时间已过
            map.put("code",203);
            return map;
        }
        //添加报名信息
        HotListDetail de = new HotListDetail();
        de.setHotId(hotId);
        de.setUserId(userId);
        //System.out.println("报名："+de);
        try {
            hotService.hotApply(de);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }

    }

    /**
     * 活动收藏
     * @param hotId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/addHotCollect/{hotId}/{userId}")
    public Map addHotCollect(@PathVariable("hotId") Long hotId,@PathVariable("userId") Long userId) {
        Map map = new HashMap();
        try {
            hotService.hotCollect(hotId,userId);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }

    /**
     * 取消活动收藏
     * @param hotId
     * @param userId
     * @return
     */
    @ResponseBody
    @RequestMapping("/reduceHotCollect/{hotId}/{userId}")
    public Map reduceHotCollect(@PathVariable("hotId") Long hotId,@PathVariable("userId") Long userId) {
        Map map = new HashMap();
        try {
            hotService.reduceHotCollect(hotId,userId);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }


    /**
     * 增加活动评论
     * @param comments
     * @return
     */
 @RequestMapping("/addHotComment")
@ResponseBody
public Map reduceHotCollect(HotComments comments) {
    Map map = new HashMap();
    try {
        comments.setCreateTime(new Date());
        hotService.addHotComment(comments);
      /*  List<HotCommentsDTO> commentsDTO=hotService.selectHotComment(comments.getHotId());
        HotDetailDTO hotDetailDTO =hotService.selectHotDetail(comments.getHotId());
        map.put("hotDetail", hotDetailDTO);
        map.put("comments",commentsDTO);*/
        map.put("code",200);
        return map;
    } catch (Exception e) {
        e.printStackTrace();
        map.put("code",500);
        return map;
    }
}


    /**
     * 展示参加活动的人数
     * @param hotId
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectHotPerson/{hotId}")
    public Map selectHotPerson(@PathVariable("hotId") Long hotId ) {
        Map map = new HashMap();
        try {
           List<String> person= hotService.selectHotPerson(hotId);
            map.put("person",person);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }

    /**
     * 活动举报
     * @param complain
     * @return
     */
    @ResponseBody
@RequestMapping("/hotReport")
public Map hotReport(Complain complain){
    Map map = new HashMap();
    try {
        complain.setComplaintype(3);//3：表示活动类型的举报
        complain.setStatus(0);//0：表示未处理
        complain.setComplainTime(new Date());
        hotService.addReport(complain);
        map.put("code",200);
        return map;
    } catch (Exception e) {
        e.printStackTrace();
        map.put("code",500);
        return map;
    }
}

    /**
     * 更改活动时显示活动的信息
     * @param hotId
     * @return
     */
    @RequestMapping("/selectHot/{hotId}")
@ResponseBody
public HotList selectHot(@PathVariable("hotId") Long hotId){

       HotList hotList= hotService.selectHotShow(hotId);

        return hotList;
}

    /**
     * 更改活动时，判断是否已经开始
     * @param hotId
     * @return
     */
    @RequestMapping("/checkTime/{hotId}")
@ResponseBody
public Map checkTime(@PathVariable("hotId") Long hotId){
        Map map = new HashMap();
        Date startTime=hotService.selectStartTime(hotId);
        Date date = new Date();
        int a=date.compareTo(startTime);
        //大于0表示当前时间活动已经开始，不能再进行更改
        if(a>0){

            map.put("code",500);
            return map;
        }
        map.put("code",200);
        return map;
    }


    /**
     * 活动更改
     * @param hotList
     * @return
     */
    @ResponseBody
    @RequestMapping("/hotUpdate")
public Map hotUpdate(HotList hotList){
        System.out.println("接收更改的活动："+hotList);
        HotList hot=hotService.selectHotShow(hotList.getId());
        Map map = new HashMap();
        hotList.setStatus(0);
        hotList.setCreateTime(hot.getCreateTime());
        String pic=hotList.getPicture();
        //去掉图片地址最后一个逗号
        String picture=pic.substring(0,pic.length()-1);
        hotList.setPicture(picture);
        try {
            hotService.updateHot(hotList);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }


    /**
     * 活动删除
     * @param hotId
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteHot/{hotId}")
    public Map deleteHot(@PathVariable("hotId") Long hotId){
        Map map = new HashMap();
        Date endTime=hotService.selectHotEndTime(hotId);
        Date date = new Date();
        int a=date.compareTo(endTime);
        //活动还没结束，不能删除
        if(a<0){
            map.put("code",201);
            return map;
        }
        try {
            hotService.deleteHot(hotId);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }


    /**
     * 删除活动评论
     * @param commentId
     * @return
     */
    @RequestMapping("/deleteComment/{id}")
    @ResponseBody
    public Map deleteComment(@PathVariable("id") Long commentId){
        Map map = new HashMap();
        try {
            hotService.deleteComment(commentId);
            map.put("code",200);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }




}
